package com.example.victory.service.sync;

import com.example.victory.entity.companyEntity.DeptAccountEntity;
import com.example.victory.mapper.DeptAndAccountMapper;
import com.example.victory.mysql.DeptAndAccountMysqlMapper;
import com.cgws.fintech.sdk.common.lock.Synchronized;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Slf4j
public class SyncDeptAndAccountService {
    @Autowired
    private DeptAndAccountMapper deptAndAccountMapper;
    @Autowired
    private DeptAndAccountMysqlMapper deptAndAccountMysqlMapper;


    @Synchronized(namespace = "inner-business-customer:syncDeptAndAccount", expireSecond = 600)
    @Transactional(rollbackFor = Exception.class)
    @Scheduled(cron = "${app.sync.detp.staff.time:0 0 6 * * ?}")
    public void SyncAccountInfo() {
        log.info(String.format("开始同步员工以及营业部概述的数据"));
        Long startDeptTime=System.currentTimeMillis();
        List<DeptAccountEntity> deptList=  deptAndAccountMapper.findDept();
        Long endDeptTime=System.currentTimeMillis();
        log.info(String.format("同步营业部概述的数据,总%s条数据,耗时:%s",deptList.size(),endDeptTime-startDeptTime));
        if(deptList!=null&&deptList.size()>0){
            deptAndAccountMysqlMapper.truncateDept();
            deptAndAccountMysqlMapper.insertDeptBatch(deptList);
            log.info(String.format("插入营业部概述的数据,总%s条数据",deptList.size()));
        }
        List<DeptAccountEntity> accountList=deptAndAccountMapper.findAccount();
        Long endAccounttTime=System.currentTimeMillis();
        log.info(String.format("同步员工概述的数据,总%s条数据,耗时:%s",accountList.size(),endAccounttTime-endDeptTime));
        if(accountList!=null&&accountList.size()>0){
            List<List<DeptAccountEntity>> accountListList= averageAssign(accountList,30);
            if(accountListList==null||accountListList.size()==0){
                return;
            }
            deptAndAccountMysqlMapper.truncateAccount();
            accountListList.stream().forEach(accounts->{
                if(accounts!=null&&accounts.size()>0){
                    deptAndAccountMysqlMapper.insertAccountBatch(accounts);
                    log.info(String.format("插入员工概述的数据,总%s条数据",accounts.size()));
                }
            });
        }
        log.info(String.format("结束同步员工以及营业部概述的数据"));
    }

    public static <T> List<List<T>> averageAssign(List<T> source, int n) {
        List<List<T>> result = new ArrayList<List<T>>();
        int remaider = source.size() % n; // (先计算出余数)
        int number = source.size() / n; // 然后是商
        int offset = 0;// 偏移量
        for (int i = 0; i < n; i++) {
            List<T> value = null;
            if (remaider > 0) {
                value = source.subList(i * number + offset, (i + 1) * number + offset + 1);
                remaider--;
                offset++;
            } else {
                value = source.subList(i * number + offset, (i + 1) * number + offset);
            }
            result.add(value);
        }
        return result;
    }

}
